package com.lw.lost.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.lw.lost.R
import com.lw.lost.databinding.ItemMessageLeftBinding
import com.lw.lost.databinding.ItemMessageRightBinding
import com.lw.lost.models.Message
import java.io.File

class MessageAdapter(
    private val currentUserId: Long
) : ListAdapter<Message, RecyclerView.ViewHolder>(DiffCallback()) {

    companion object {
        private const val VIEW_TYPE_LEFT = 1
        private const val VIEW_TYPE_RIGHT = 2
    }

    override fun getItemViewType(position: Int): Int {
        return if (getItem(position).senderId == currentUserId) {
            VIEW_TYPE_RIGHT
        } else {
            VIEW_TYPE_LEFT
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            VIEW_TYPE_LEFT -> LeftViewHolder(
                ItemMessageLeftBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
            VIEW_TYPE_RIGHT -> RightViewHolder(
                ItemMessageRightBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
            else -> throw IllegalArgumentException("Invalid view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val message = getItem(position)
        when (holder) {
            is LeftViewHolder -> holder.bind(message)
            is RightViewHolder -> holder.bind(message)
        }
    }

    inner class LeftViewHolder(
        private val binding: ItemMessageLeftBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(message: Message) {
            binding.apply {
                if (!message.imageUrl.isNullOrEmpty()) {
                    ivImage.visibility = View.VISIBLE
                    tvMessage.visibility = View.GONE
                    
                    Glide.with(ivImage)
                        .load(File(message.imageUrl))
                        .into(ivImage)
                        
                    ivImage.setOnClickListener {
                        // TODO: 实现查看大图功能
                    }
                } else {
                    ivImage.visibility = View.GONE 
                    tvMessage.visibility = View.VISIBLE
                    tvMessage.text = message.content
                }
                
                tvTime.text = message.sendTime
            }
        }
    }

    inner class RightViewHolder(
        private val binding: ItemMessageRightBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(message: Message) {
            binding.apply {
                if (!message.imageUrl.isNullOrEmpty()) {
                    ivImage.visibility = View.VISIBLE
                    tvMessage.visibility = View.GONE
                    
                    Glide.with(ivImage)
                        .load(File(message.imageUrl))
                        .into(ivImage)
                        
                    ivImage.setOnClickListener {
                        // TODO: 实现查看大图功能
                    }
                } else {
                    ivImage.visibility = View.GONE 
                    tvMessage.visibility = View.VISIBLE
                    tvMessage.text = message.content
                }
                
                tvTime.text = message.sendTime
            }
        }
    }

    private class DiffCallback : DiffUtil.ItemCallback<Message>() {
        override fun areItemsTheSame(oldItem: Message, newItem: Message) =
            oldItem.messageId == newItem.messageId

        override fun areContentsTheSame(oldItem: Message, newItem: Message) =
            oldItem == newItem
    }
} 